//
//  TitleView.m
//  niminban
//
//  Created by zhouteng on 16/10/1.
//  Copyright © 2016年 zhouteng. All rights reserved.
//

#import "TitleView.h"
#import "NSString+HTML.h"
#import "Masonry.h"
#import "COLORS.h"

#define TOP_MARGIN 12
#define LEFT_MARGIN 12

@implementation TitleView

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
         self.contentView.backgroundColor = GRAY_COLOR;
        
        UIView *view = [[UIView alloc]init];
        view.backgroundColor = [UIColor whiteColor];
        _lblId = [[UILabel alloc]init];
        _lblNo = [[UILabel alloc]init];
        _lblTime = [[UILabel alloc]init];
        _lblReplyNum = [[UILabel alloc]init];
        _txtContent = [[UILabel alloc]init];
        _txtContent.numberOfLines = 0;
        
        [self updateFont];
        
        view.layer.cornerRadius = 5.0;
        view.layer.masksToBounds = NO;
        view.layer.shadowColor = [[UIColor blackColor] colorWithAlphaComponent:0.2].CGColor;
        view.layer.shadowOffset = CGSizeMake(0, 0);
        view.layer.shadowOpacity = 0.8;

        [self.contentView addSubview:view];
        [self.contentView addSubview:_lblId];
        [self.contentView addSubview:_lblNo];
        [self.contentView addSubview:_lblTime];
        [self.contentView addSubview:_txtContent];
        [self.contentView addSubview:_lblReplyNum];
        
        UIEdgeInsets padding = UIEdgeInsetsMake(4, 8, 4, 8);
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).with.insets(padding);
        }];
        
        [_lblId mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@0).offset(TOP_MARGIN);
            make.left.equalTo(@0).offset(LEFT_MARGIN);
            make.width.equalTo(@[_lblNo,_lblTime]);
            
        }];
        
        [_lblNo mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@0).offset(TOP_MARGIN);
            make.left.equalTo(_lblId.mas_right);
        }];
        
        [_lblTime mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@0).offset(TOP_MARGIN);
            make.left.equalTo(_lblNo.mas_right);
            make.right.equalTo(@0).offset(-LEFT_MARGIN);
        }];
        
        [_txtContent mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_lblId.mas_bottom).offset(TOP_MARGIN);
            make.left.equalTo(@0).offset(LEFT_MARGIN);
            make.right.equalTo(@0).offset(-LEFT_MARGIN);
            make.bottom.equalTo(_lblReplyNum.mas_top).offset(-TOP_MARGIN);
        }];
        
        [_lblReplyNum mas_makeConstraints:^(MASConstraintMaker *make) {
            //make.top.equalTo(_txtContent).offset(TOP_MARGIN);
            make.right.equalTo(@0).offset(-LEFT_MARGIN);
            make.bottom.equalTo(@0).offset(-8);
        }];
    }
    return self;
}

-(void)updateFont{
    UIFont *lblFont = [UIFont systemFontOfSize:12];
    UIFont *contentFont = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    UIColor *lblColor = [UIColor grayColor];
    _lblId.font = lblFont;
    _lblId.textColor = lblColor;
    _lblNo.font = lblFont;
    _lblNo.textColor = lblColor;
    _lblTime.font = lblFont;
    _lblTime.textColor = lblColor;
    _lblReplyNum.font = lblFont;
    _lblReplyNum.textColor = lblColor;
    _txtContent.font = contentFont;
}

-(void)updateConstraints{
    [super updateConstraints];
}

-(void)setTitle:(Title *)title{
    _lblId.text = title.userid;
    _lblNo.text = title.id;
    _lblTime.text = title.now;
    _lblReplyNum.text = title.replyCount;
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[title.content dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    _txtContent.attributedText = attrStr;
}

@end
